package com.android.gufengmall.video;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.SearchAdapter;
import com.android.gufengmall.bean.GoodsBean;
import com.android.gufengmall.bean.VideoBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.GoodsBeanDao;
import com.android.gufengmall.gen.VideoBeanDao;
import com.android.gufengmall.mall.MallDetailsActivity;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseActivity implements SearchAdapter.VideoListener {

    private static final String TAG = "SearchActivity";

    private App mApp;
    private DaoSession mDaoSession;
    private VideoBeanDao mVideoBeanDao;
    private GoodsBeanDao mGoodsBeanDao;

    private SearchAdapter mSearchAdapter;
    private List<VideoBean> mSearchVideoBeanList;
    private List<GoodsBean> mSearchGoodsBeanList;

    private EditText mEdtSearch;
    private RecyclerView mRvSearch;
    private boolean mIsVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mSearchAdapter.setOnVideoListener(SearchActivity.this);

        mEdtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mIsVideo) {
                    mSearchVideoBeanList.clear();
                    List<VideoBean> bloodsLikeDate = getVideoLikeDate(s.toString().trim());
                    mSearchVideoBeanList.addAll(bloodsLikeDate);
                } else {
                    mSearchGoodsBeanList.clear();
                    List<GoodsBean> goodsLikeDate = getGoodsLikeDate(s.toString().trim());
                    mSearchGoodsBeanList.addAll(goodsLikeDate);
                }
                mSearchAdapter.notifyDataSetChanged();

            }
        });
    }

    private void initView() {
        mEdtSearch = findViewById(R.id.edt_search);
        mRvSearch = findViewById(R.id.rv_search);

    }

    private void initData() {
        mSearchVideoBeanList = new ArrayList<>();
        mSearchGoodsBeanList = new ArrayList<>();

        mIsVideo = getIntent().getExtras().getBoolean("isVideo");

        mApp = (App) this.getApplication();
        mDaoSession = mApp.getDaoSession();
        mVideoBeanDao = mDaoSession.getVideoBeanDao();
        mGoodsBeanDao = mDaoSession.getGoodsBeanDao();

        mRvSearch.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL ,false));
        mSearchAdapter = new SearchAdapter(this,mIsVideo, mSearchVideoBeanList,mSearchGoodsBeanList);
        mRvSearch.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRvSearch.setAdapter(mSearchAdapter);

    }

    @Override
    public void onSearchVideoClick(String videoTitle) {
        Intent intent;
        if (mIsVideo) {
            intent = new Intent(this, VideoDetailsActivity.class);
        } else {
            intent = new Intent(this, MallDetailsActivity.class);
        }
        intent.putExtra("position", videoTitle);
        startActivity(intent);
    }

    public List<VideoBean> getVideoLikeDate(String measureDate) {
        List<VideoBean> list = mVideoBeanDao.queryBuilder()
                .where(
                        VideoBeanDao.Properties.VideoTitle.like(measureDate + "%")
                )
                .orderDesc(VideoBeanDao.Properties.VideoTitle)
                .build()
                .list();

        return list;
    }

    public List<GoodsBean> getGoodsLikeDate(String measureDate) {
        List<GoodsBean> list = mGoodsBeanDao.queryBuilder()
                .where(
                        GoodsBeanDao.Properties.Goods_name.like(measureDate + "%")
                )
                .orderDesc(GoodsBeanDao.Properties.Goods_name)
                .build()
                .list();

        return list;
    }
}